package com.example.newsmanager.controller;

import com.example.newsmanager.common.Result;
import com.example.newsmanager.entity.Collect;
import com.example.newsmanager.entity.User;
import com.example.newsmanager.service.CollectService;
import com.example.newsmanager.service.NewsService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/collect")
public class CollectController {
    @Autowired
    private CollectService collectService;
    @Autowired
    private NewsService newsService;
    @PostMapping("/add")
    public Result<?> addCollect(@RequestBody Collect collect, HttpServletRequest request){
        User currentUser = (User) request.getAttribute("currentUser");
        collect.setUserId(currentUser.getUserId());
        collectService.addCollect(collect);
        newsService.addCollect(collect);
        return Result.success(collect,"收藏成功");
    }

    @PostMapping("/isCollected")
    public Result<?> isCollected(@RequestBody Collect collect,HttpServletRequest request){
        User currentUser = (User) request.getAttribute("currentUser");
        Boolean isCollected = collectService.isCollected(collect,currentUser.getUserId());
       if (isCollected){
          return Result.fail(true,"已经添加此为收藏");
       }
        else {
          return Result.success(false,"未添加此为收藏");
       }
    }
    @PostMapping("/delete")
    public Result<?> deleteCollected(@RequestBody Collect collect,HttpServletRequest request){
        User currentUser = (User) request.getAttribute("currentUser");
        collectService.deleteCollect(collect,currentUser.getUserId());
        return Result.success("取消收藏成功");
    }


}
